var stationTotalKWHDayData = [];
var totalPage = 0;
var totalCnt = 0;
var lastheaderNameEN = undefined;
var chargingStationNameList = [];
var chargingKWHTotalList = [];
var chargingCountTotalList = [];
var totalChargingDTTMList = [];
var totalChargingKWHList = [];
var chargingKWHThisList = [];
var dataList = [];
var myChart = "";

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("stationTotalKWHDayThead", stationTotalKWHDayTable, search);
	scrollTableSuspend("scrollBody", "stationTotalKWHDayTable", "stationTotalKWHDayTable1", "stationTotalKWHDayThead", "scrollHeight");
	selectDateTime("startDate");
	selectDateTime("endDate");
	$("#startChargingTime").val(date2String(getSystemTime()) + " 00:00");
	$("#endChargingTime").val(date2String(getSystemTime()) + " 23:59");
	search();
});
function search(headerNameEN){
	chargingStationNameList = [];
	chargingKWHTotalList = [];
	chargingCountTotalList = [];
	totalChargingDTTMList = [];
	totalChargingKWHList =[];
	chargingKWHThisList = [];
	var startChargingTime = $("#startChargingTime").val();
	var endChargingTime = $("#endChargingTime").val();
	if(lastheaderNameEN != headerNameEN){
		clearOrder(lastheaderNameEN);
		lastheaderNameEN = headerNameEN;
	}
	if (stringToDate(startChargingTime) > stringToDate(endChargingTime)){
		showMessge("开始时间不能大于结束时间！");
		$("#contenter").attr("hidden",true);
	} else {
		$ajax('searchStationTotalKWHDay', 'stationTotalKWHDaySearch', searchStationTotalKWHDay);
	}
}
function searchStationTotalKWHDay(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	$("#totalCount").html(data.totalChargingCount);
	$("#totalKWH").html(data.totalChargingKWHDay);
	$("#totalTime").html(formatTime(data.totalChargingTime) || "0");
	stationTotalKWHDayData = data && data.stationTotalKWHDay || [];
	createTableBody("stationTotalKWHDayTbody", stationTotalKWHDayTable, stationTotalKWHDayData);
	for(var i=0; i<stationTotalKWHDayData.length; i++){
		if( stationTotalKWHDayData[i].chargingStationName != "北京怀柔公交场站"){
			chargingStationNameList.push(stationTotalKWHDayData[i].chargingStationName.replace(/[^0-9]/ig,"")+"路");
		}else{
			chargingStationNameList.push("怀柔");
		}
		chargingKWHTotalList.push(stationTotalKWHDayData[i].chargingKWHTotal);
		chargingCountTotalList.push(stationTotalKWHDayData[i].chargingCountTotal);
		totalChargingDTTMList.push(stationTotalKWHDayData[i].totalChargingDTTM);
		totalChargingKWHList.push(stationTotalKWHDayData[i].totalChargingKWH);
		chargingKWHThisList.push(stationTotalKWHDayData[i].chargingKWHThis);
	}
	changeEchart();
	setTimeout(hideLoading, 500);
}
function changeEchart(){
	$("#histogram").empty();
	if(chargingStationNameList.length == 0){
		$("#contenter").attr("hidden",true);
		return;
	}
	$("#contenter").attr("hidden",false);
	myChart = echarts.init(document.getElementById('histogram'));
	var listName = $("#selectAllList").val();
	var theadName = undefined;
	var color = undefined;
	if(listName == "chargingKWHTotalList"){
		dataList = chargingKWHTotalList;
		theadName = "充电电量(KWH)";
		color = '#09be09';
	} else if(listName == "chargingCountTotalList"){
		dataList = chargingCountTotalList;
		theadName = "充电次数(次)";
		color = '#01b2ac';
	} else if(listName == "totalChargingDTTMList"){
		dataList = totalChargingDTTMList;
		theadName = "充电时长(秒)";
		color = '#fe902f';
	} else if(listName == "totalChargingKWHList"){
		dataList = totalChargingKWHList;
		theadName = "平均充电量(KWH)";
		color = '#5fb1ff';
	} else if(listName == "chargingKWHThisList"){
		dataList = chargingKWHThisList;
		theadName = "最大充电量(KWH)";
		color = '#5fb1ff';
	}
    var option = {
            tooltip: {
                trigger: 'axis'
            },
            xAxis: [
                {
                    type: 'category',
                    name: '充电站名称',
                    data: chargingStationNameList
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: theadName,
                    min: 0,
                    axisLabel: {
                        formatter: '{value} '
                    }
                }
            ],
            series: [
                {
                    name:theadName,
                    type:'bar',
                    barWidth:10,
                    itemStyle: {
                        normal : {
                            color: color,
                            label: {
                                show: true,
                                position: 'top',
                                formatter: '{c}'
                            }
                        }
                    },
                    data:dataList
                }
            ]
        };
    myChart.setOption(option);
}